﻿//Prev-Back buttons
plugin.plugin_flags=gddPluginFlagToolbarBack | gddPluginFlagToolbarForward;
plugin.onCommand=NextPrev;
function NextPrev(cmd){
  if (cmd==gddCmdToolbarBack){
    showPrev();
  }else if (cmd==gddCmdToolbarForward){
    showNext();
  }
}

//Display settings
isDocked = true;
plugin.onDisplayTargetChange = onDisplayTargetChange;

function mainOnOpen() {
//Constants
IPall=new Array();
current=-1;
//Saved timers
  var time = options.getValue("time_box");
  if (time==''){
	  time='300';
  }
  var display = options.getValue("display_box");
  if (display==''){
	  display='5';
  }
ClearNext();
ClearRefresh();
//Thank you, James!
//http://groups.google.com/group/Google-Desktop-Developer/browse_thread/thread/36103e797ac29da5
var wmiService = GetObject("winmgmts:\\\\.\\root\\cimv2"); 
var adapters = wmiService.ExecQuery("Select IPAddress From Win32_NetworkAdapterConfiguration Where IPEnabled = True"); 
//iterate through adapters
 for (var e = new Enumerator(adapters); !e.atEnd(); e.moveNext()) { 
  var adapter = e.item();
  // Iterate through IP addresses for an adapter. 
  // Though the property name is "IPAddress" it is actually an array 
  // of all addresses for the adapter. 
  if (adapter.IPAddress != null) { 
    var addresses = VBArray(adapter.IPAddress).toArray();
    IPall=IPall.concat(addresses);
    for (var i = 0; i < addresses.length; ++i) { 
      var address = addresses[i];
    } 
  } 
 }

  if (IPall.length==0){
     ip_box.innerText=OFFLINE;
  }else{
    showNext();
    next_timer=setInterval(showNext,(display*1000)); //next timer
  }
  //recheck timer
  ref_timer=setInterval(mainOnOpen,(time*1000));
}

function ClearNext(){
//ShowNext
if (typeof next_timer != 'undefined'){
    clearInterval(next_timer);
}
}
function ClearRefresh(){
//Refresh
if (typeof ref_timer != 'undefined'){
    clearInterval(ref_timer);
}
}
function showNext(){
  var last=IPall.length-1;
  var next=current+1;
  if (next>last){
    next=0;
  }
  showIP(next);
}

function showPrev(){
  var last=IPall.length-1;
  var next=current-1;
  if (next<0){
    next=last;
  }
  showIP(next);
}

function showIP(position){
    view.caption=IPall[position];
    ip_box.innerText=IPall[position];
    if (IPall.length>1){
      ip_box.opacity=0;
      beginAnimation(function() { setOpacity(); }, 0, 255, 500);
    }
    current=position;
    if (isDocked){
      box_img.src='';
      view.height=110;
      ip_box.size=16;
      ip_box.height=70;
      my_ip.size=10;
    }else{
      view.height=134;
      box_img.src='stock_images/background.png';
      ip_box.size=12;
      ip_box.height=47;
      my_ip.size=8;
    }
}


function setOpacity(){
 ip_box.opacity = event.value;
}

function onDisplayTargetChange(displayTarget) {
  if (displayTarget == gddTargetSidebar) {
    isDocked = true;
    showNext();
  } else if (displayTarget == gddTargetFloatingView) {
    isDocked = false;
    showNext();
  }
}
function PopOut(){
   ip_box.color="#000000";
   my_ip.color="#000000";
}
function PopIn(){
   ip_box.color="#FFFFFF";
   my_ip.color="#FFFFFF";
}